
import React from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { useModel } from 'umi';
import { Table, Card, Row, Col } from 'antd';


const docMarkdownDefaultPage: React.FC<{}> = (props: any) => {
  const { initialState } = useModel('@@initialState');
  if (!initialState || !initialState.settings) {
    return null;
  }
  const getChildrenInfo = (children: any, rootKey: string) => {
    var obj = {};
    obj["count"] = 0;
    obj["children"] = [];
    obj["children"][0] = { key: 11, groupName: '', method: 'get', count: 10, };
    obj["children"][1] = { key: 11, groupName: '', method: 'post', count: 10, };
    if (children != undefined && children.length > 0) {
      var getCount = 0;
      var postCount = 0;
      for (var index in children) {
        var apiInfo = children[index];
        var apiDetail = apiInfo.apiDetail;
        if (apiDetail != undefined) {
          if (apiDetail.method != undefined && apiDetail.method == "post") {
            postCount = postCount + 1;
          }
          if (apiDetail.method != undefined && apiDetail.method == "get") {
            getCount = getCount + 1;
          }
        } else {
          var childrenx = apiInfo.children;
          var childrenxItems = getChildrenInfo(childrenx, rootKey + index);
          if (childrenxItems != undefined) {
            var childrens = childrenxItems["children"];
            var childrenGetCount = childrens[0]["count"];
            var childrenPostCount = childrens[1]["count"];
            postCount = postCount + childrenPostCount;
            getCount = getCount + childrenGetCount;
          }
        }
      }
      obj["count"] = getCount + postCount;
      obj["children"][0].count = getCount;
      obj["children"][0].key = rootKey + 1;
      obj["children"][1].count = postCount;
      obj["children"][1].key = rootKey + 2;
    }
    return obj;
  }
  const statistsApiInfo = () => {
    var menuData = initialState.menuData;
    if (menuData != undefined) {
      var menuStaticsData = [];
      var nm: number = 0;
      for (var index in menuData) {
        var menuItem = menuData[index];
        var name = menuItem.name;
        if (name != undefined) {
          var method = menuItem.method;
          var path = menuItem.path;
          if (path != undefined && path == "/") {
            method = "";
          }
          nm = nm + 1;
          var obj = {};
          obj["key"] = nm + 1;
          obj["groupName"] = name;
          obj["method"] = method;
          var children = menuItem.children;
          if (children != undefined) {
            var childrenInfo = getChildrenInfo(children, obj["key"]);
            obj["count"] = childrenInfo["count"];
            obj["children"] = childrenInfo["children"];
          } else {
            obj["count"] = '0';
          }
          menuStaticsData[nm] = obj;
        }
      }
      return menuStaticsData;
    }

    return [];
  }

  const columns: any = [
    {
      title: '接口类别名称',
      dataIndex: 'groupName',
      key: 'groupName',
      align: 'center',
      render: (text: React.ReactNode) => <a style={{ float: "left" }}>{text}</a>,
    },
    {
      title: '请求方式',
      dataIndex: 'method',
      key: 'method',
      align: 'center',
      width: '25%',
      render: (text: React.ReactNode) => <a>{text}</a>,
    },
    {
      title: '个数',
      dataIndex: 'count',
      key: 'count',
      align: 'center',
      width: '25%',
      render: (text: React.ReactNode) => <a style={{ float: "right" }}>{text}</a>,
    },
  ];

  // const data = [
  //   {
  //     key: 1,
  //     groupName: 'token',
  //     method: '',
  //     count: 20,
  //     children: [
  //       {
  //         key: 11,
  //         groupName: '',
  //         method: 'get',
  //         count: 10,
  //       },
  //       {
  //         key: 12,
  //         groupName: '',
  //         method: 'post',
  //         count: 10,
  //       },
  //     ],
  //   },
  //   {
  //     key: 2,
  //     groupName: '系统接口',
  //     method: '',
  //     count: 20,
  //     children: [
  //       {
  //         key: 21,
  //         groupName: '',
  //         method: 'get',
  //         count: 10,
  //       },
  //       {
  //         key: 22,
  //         groupName: '',
  //         method: 'post',
  //         count: 10,
  //       },
  //     ],
  //   },
  // ];

  return (
    <PageHeaderWrapper title="接口统计" content='分类统计接口信息.'>
      <Card title="" bordered={false} style={{ paddingLeft: 0, paddingRight: 0,paddingTop:20,paddingBottom:20 }}>
        <Row>
          <Col span={3}></Col>
          <Col span={18}><Table size={"small"} bordered columns={columns} dataSource={statistsApiInfo()} pagination={false} defaultExpandAllRows={true} /></Col>
          <Col span={3}></Col>
        </Row>
      </Card>

    </PageHeaderWrapper>
  );
};

export default docMarkdownDefaultPage;